
/* JavaScript content from js/events.js in folder common */


function registerEvents(){
	/**
	 * Event handler
	 * 
	 */

	jq(".modifyThoughtBtn").click(function(){
		WL.Logger.debug("Thought::delete thought");
		jq.mobile.showPageLoadingMsg();
		
		var id = jq("#thoughtid").text();
		var thought_value= jq("#thoughtEditArea").attr("value");
		var action_value= jq("#thoughtActionEditArea").attr("value");
		
		var thoughtsInvocationData = {
				adapter: "DataControllerAdaptor",
				procedure: "updateThought",
				parameters: [id, thought_value, action_value]
		};
		
		WL.Client.invokeProcedure(thoughtsInvocationData, {
			onSuccess: function(data){
				if (!data || !data.invocationResult.isSuccessful)
					showErrorMessage("Could not retrieve correct data from server");
				WL.Logger.debug("Thought::update thought successfully");
				jq.mobile.hidePageLoadingMsg();
				thoughts = null;
				jq("#thoughtEditArea").val("");
				jq("#thoughtActionEditArea").val("");
				jq.mobile.changePage("#Category_ThoughtsListPage");
			}, 
			onFailure: loadItemsFail
		});
		
	});
	
	jq(".shareThoughtBtn").click(function(){
		WL.Logger.debug("Thought::share thought");
		jq.mobile.showPageLoadingMsg();
		
		var id = jq("#thoughtid").text();
		var record = searchThought(id);
		var shared=0;
		if(record.shared==0){
			shared=1;
		}else if(record.shared==0){
			shared=0;
		}
		
		var btn = jq(".shareThoughtBtn");
		if(shared==0){
			btn.attr("data-icon","minus");
			btn.children().children().removeClass('ui-icon-minus').addClass('ui-icon-star');
		}else if(shared==1){
			btn.attr("data-icon","star");
			btn.children().children().removeClass('ui-icon-star').addClass('ui-icon-minus'); 
		}
		
		var thoughtsInvocationData = {
				adapter: "DataControllerAdaptor",
				procedure: "shareThought",
				parameters: [id, shared]
		};
		
		WL.Client.invokeProcedure(thoughtsInvocationData, {
			onSuccess: function(data){
				if (!data || !data.invocationResult.isSuccessful)
					showErrorMessage("Could not retrieve correct data from server");
				WL.Logger.debug("Thought::update thought successfully");
				
				jq.mobile.hidePageLoadingMsg();
				thoughts = null;
				sharedThoughts = null;
				jq.mobile.changePage("#Category_ThoughtsListPage");
			}, 
			onFailure: loadItemsFail
		});
	});
	
	jq(".deleteThoughtBtn").click(function(){

		WL.Logger.debug("Thought::delete thought");
		jq.mobile.showPageLoadingMsg();
		
		var id = jq("#thoughtid").text();
		var thoughtsInvocationData = {
				adapter: "DataControllerAdaptor",
				procedure: "deleteThought",
				parameters: [id]
		};
		
		WL.Client.invokeProcedure(thoughtsInvocationData, {
			onSuccess: function(data){
				if (!data || !data.invocationResult.isSuccessful)
					showErrorMessage("Could not retrieve correct data from server");
				WL.Logger.debug("Thought::create successfully");
				jq.mobile.hidePageLoadingMsg();
				thoughts = null;
				jq.mobile.changePage("#Category_ThoughtsListPage");
			}, 
			onFailure: loadItemsFail
		});
	});
	
	jq("#submitNewThoughtBtn").click(function(){
			WL.Logger.debug("Thought::new thought");
			jq.mobile.showPageLoadingMsg();
			
			var thought_value= jq("#thoughtOfThoughtArea").attr("value");
			var action_value= jq("#actionOfThoughtArea").attr("value");
			
			var thoughtsInvocationData = {
					adapter: "DataControllerAdaptor",
					procedure: "createThought",
					parameters: [owner_id, thought_value, action_value]
			};
			
			WL.Client.invokeProcedure(thoughtsInvocationData, {
				onSuccess: function(data){
					if (!data || !data.invocationResult.isSuccessful)
						showErrorMessage("Could not retrieve correct data from server");
					WL.Logger.debug("Thought::create successfully");
					jq.mobile.hidePageLoadingMsg();
					thoughts = null;
					jq("#thoughtOfThoughtArea").val("");
					jq("#actionOfThoughtArea").val("");
					jq.mobile.changePage("#Category_ThoughtsListPage");
				}, 
				onFailure: loadItemsFail
			});
	});
	
	//Listen for any attempts to call changePage().
	jq(document).bind( "pagebeforechange", function( e, data ) {
		if ( typeof data.toPage === "string" ) {
			var url = jq.mobile.path.parseUrl( data.toPage );
			var categoryRegex = /^#Category_/;
			var contentRegex = /^#Content_/;

			// Load and insert category list dynamically 
			if ( url.hash.search(categoryRegex) !== -1 ) {
				// Replace category behavior to insert data from database
				if(url.hash=="#Category_GoalsListPage"){
					if(!goals||goals.length==0){
						loadGoals(owner_id);
					}else{
						displayGoals(goals);
					}
				}else if(url.hash=="#Category_ThoughtsListPage"){
					if(!thoughts||thoughts.length==0){
						loadThoughts(owner_id);
					}else{
						displayThoughts(thoughts);
					}
				}else if(url.hash=="#Category_SharedThoughtsPage"){
					if(!sharedThoughts||sharedThoughts.length==0){
						loadSharedThoughts();
					}else{
						displaySharedThoughts(sharedThoughts);
					}
				}
			}
			
			// Load and insert content dynamically 
			if ( url.hash.search(contentRegex) !== -1 ) {
				//#Content_ThoughtPage?id=${id}
				var contentInfoRegex = /(#Content_\w+)\?id=(\d+)/;
				var result = contentInfoRegex.exec(url.hash);
				if (result != null) {
					if(result[1]=="#Content_GoalPage"){
						var record = searchGoal(result[2]);
						//update content page title
						jq("#GoalTitle").html(record.update_year);
						//update content 
						jq("#GoalContent").empty();
						
						var data = jq("<label for='goalEditArea'><h3>发展目标:</h3></label>"+
						"<textarea name='textarea' id='goalEditArea' readonly='true'>"+record.goal+
						"</textarea><label for='goalActionEditArea'><h3>行动计划:</h3></label>" +
						"<textarea name='textarea' id='goalActionEditArea' readonly='true'>"+record.action+"</textarea><div id='goalid' style='visibility:hidden'>"+record.id+"</div>");
						jq("#GoalContent").append(data);
						
					}else if(result[1]=="#Content_ThoughtPage"){
						var record = searchThought(result[2]);			
						jq("#ThoughtTitle").html(record.update_date);
						jq("#ThoughtContent").empty();
						var data = jq("<label for='thoughtEditArea'><h3>解决挑战:</h3></label>"+
								"<textarea name='textarea' id='thoughtEditArea'>"+record.thought+
								"</textarea><label for='thoughtActionEditArea'><h3>行为模式:</h3></label>" +
								"<textarea name='textarea' id='thoughtActionEditArea'>"+record.action+"</textarea><div id='thoughtid' style='visibility:hidden'>"+record.id+"</div>");
						jq("#ThoughtContent").append(data);
						
					}else if(result[1]=="#Content_SharedThoughtPage"){
						var record = searchSharedThought(result[2]);
						jq("#SharedThoughtTitle").html(record.update_date);
						jq("#SharedThoughtContent").empty();
						var data = jq("<label><h3>解决挑战:</h3></label>"+
								"<textarea name='textarea' readonly='true'>"+record.thought+
								"</textarea><label><h3>行为模式:</h3></label>" +
								"<textarea name='textarea' readonly='true'>"+record.action+"</textarea><div id='sharedthoughtid' style='visibility:hidden'>"+record.id+"</div>");
						jq("#SharedThoughtContent").append(data);
					}
				}
			}
		}
	});
}